//
//  JXSeletedDateEditView.m
//  TestCollectionView
//
//  Created by 张俊正（总行零售网金） on 2019/4/24.
//  Copyright © 2019 sunshinfight@163.com. All rights reserved.
//

#import "JXSelectedDateEditView.h"
#import <Masonry.h>

@implementation JXSelectedDateEditView{
    UIButton *_submitButton;
    UILabel *_startDateLabel;
    UILabel *_endDateLabel;
    UILabel *_typeIndicatorLabel;
    JXCalendarModel *_startDate;
    JXCalendarModel *_endDate;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initUI];
        [self configUI];
    }
    return self;
}

- (void)initUI
{
    _submitButton = [UIButton new];
    _startDateLabel = [UILabel new];
    _endDateLabel = [UILabel new];
    _typeIndicatorLabel = [UILabel new];
}

- (void)configUI
{
    self.backgroundColor = UIColor.whiteColor;
    self.layer.shadowColor = UIColor.grayColor.CGColor;
    self.layer.shadowRadius = 5;
    self.layer.shadowOpacity = 0.15;
    
    [self addSubview:_submitButton];
    _submitButton.backgroundColor = UIColor.whiteColor;
    CAGradientLayer *colorLayer = [CAGradientLayer new];
    colorLayer.colors = @[[UIColor colorWithRed:255.0/255.0 green:142.0/255.0 blue:0 alpha:1.0], [UIColor colorWithRed:255.0/255.0 green:79.0/255.0 blue:0 alpha:1.0]];
    colorLayer.startPoint = CGPointMake(0, 0);
    colorLayer.endPoint = CGPointMake(1, 0);
    colorLayer.locations = @[@0, @1];
    colorLayer.frame = _submitButton.bounds;
    [_submitButton.layer addSublayer:colorLayer];
    
    [_submitButton.layer addSublayer:colorLayer];
    [_submitButton setTitle:@"提交" forState:UIControlStateNormal];
    _submitButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [_submitButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    _submitButton.backgroundColor = [UIColor colorWithRed:255.0/255.0 green:142.0/255.0 blue:0 alpha:1.0];
    [_submitButton addTarget:self action:@selector(submitButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [_submitButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.mas_left).offset(15);
        make.right.mas_equalTo(self.mas_right).offset(-15);
        make.bottom.mas_equalTo(self.mas_bottom).offset(-13);
        make.height.mas_equalTo(44);
    }];
    
    [self addSubview:_startDateLabel];
    _startDateLabel.backgroundColor = UIColor.whiteColor;
    _startDateLabel.font = [UIFont systemFontOfSize: 14];
    _startDateLabel.textColor = [UIColor colorWithRed:40.0/255.0 green:40.0/255.0 blue:40.0/255.0 alpha:1.0];
    [_startDateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(15);
        make.height.mas_equalTo(20);
    }];
    
    [self addSubview:_endDateLabel];
    _endDateLabel.backgroundColor = UIColor.whiteColor;
    _endDateLabel.font = [UIFont systemFontOfSize: 14];
    _endDateLabel.textColor = [UIColor colorWithRed:40.0/255.0 green:40.0/255.0 blue:40.0/255.0 alpha:1.0];
    [_endDateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.top.mas_equalTo(15);
        make.height.mas_equalTo(20);
    }];
    
    [self addSubview:_typeIndicatorLabel];
    _typeIndicatorLabel.text = @"整天";
    _typeIndicatorLabel.backgroundColor = UIColor.whiteColor;
    _typeIndicatorLabel.font = [UIFont systemFontOfSize:13];
    _typeIndicatorLabel.textColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1.0];
    [_typeIndicatorLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.top.mas_equalTo(self->_endDateLabel.mas_bottom).offset(15);
    }];
}

- (void)configWithModels: (NSArray<JXCalendarModel* >*)models
{
    if (models.count == 0) {
        _startDateLabel.text = nil;
        _endDateLabel.text = nil;
        _startDate = nil;
        _endDate = nil;
        return;
    }
    _startDate = models[0];
    _endDate = models.lastObject;

    _startDateLabel.text = [NSString stringWithFormat:@"%d月%d日", _startDate.month, _startDate.day];
    _endDateLabel.text = [NSString stringWithFormat:@"%d月%d日", _endDate.month, _endDate.day];
}

- (void)submitButtonAction
{
    [self.delegate JXSelectedDateEditView:self submitButtonTappedWithStartDate:_startDate endDate:_endDate];
}

- (void)resetDateSource
{
    _startDateLabel.text = nil;
    _endDateLabel.text = nil;
    _startDate = nil;
    _endDate = nil;
}

@end
